/**
 * 
 * @param {type} $
 * @returns {undefined}
 */
;(function($){
    
    flat = {};
    
    /**
     * 
     * @param {string} _namespace
     * @returns {object} Reference to the created namespace
     */
    flat.namespace = function(_namespace){
            
        var namePieces = _namespace.split('.');
        var step = window;
        for(var i = 0; i < namePieces.length; i++){

            var piece = namePieces[i];
            step[piece] = step[piece] || {};
            step = step[piece];

        }

        return step;

    };
    
    //loads script in a context
    flat.script = new (function(){
        
        var _queue = [];
        
        /**
         * Adds a callback to the script queue
         * 
         * @param {callback} cb
         * @param {boolean} persistent This callback will be removed after called?
         * @returns {undefined}
         */
        this.load = function(cb, persistent){
            
            persistent = persistent || false;
            
            if(typeof cb !== "function"){
                throw "Invalid callback.";
            }
            
            _queue.push({callback: cb, persistent: persistent});
            
        };
        
        /**
         * Proccess the script callback.
         * After calling each script, removes it from the queue, unless it's
         * persistent
         * 
         * @param {object} _context
         * @param {object} _values
         * @param {object} _window
         * @returns {undefined}
         */
        this.dispatch = function(_context, _values, _window){
            
            _context = _context || document;
            _values = _values || {};
            _window = _window || window;
            
            for(var i in _queue){
                
                var item = _queue[i];
                item.callback($(_context), _values, _window);
                if(!item.persistent)
                    delete _queue[i];
                
            }
            
        };
        
    });
    
})(jQuery);